<html>
<head>
<link rel="stylesheet" href="calendar-style.css" />
<script type="text/javascript" src="calendar_conf.js"></script>
<script type="text/javascript">
function map(code,list) {
  var result = [];
  for (var i = 0; i < list.length; i++) {
    result.push( code(list[i]));
  };
  return result;
};

function grep(code,list) {
  var result = [];
  for (var i = 0; i < list.length; i++) {
    if (code(list[i])) {
      result.push( list[i]);
    };
  };
  return result;
};

function strftime(format,date) {
  var result = format;
  result = result.replace( /%Y/, date.getFullYear());
  result =  result.replace( /%m/, fnl(date.getMonth()+1,2));
  result =  result.replace( /%d/, fnl(date.getDate(),2));
  return result;
};

function fnl(n, l) { var r = "" + n; while (r.length < l) { r = "0" + r; } return r; }


function strptime(format,text) {
  var result = new Date();
  // quote meta chars
  var s = '^' + format.replace( /([][.\*])/g, '\\$1') + '$';
  var re_format = new RegExp( s.replace(/%[Ymd]/g,'(\\d+)'));
  var match = text.match( re_format );

  if (match) {
    // Throw away the full string that appears for some weird reason
    match.shift();
    // we have a valid, matching date
    var date_parts = new Object();
    date_parts['Y'] = result.getFullYear();
    date_parts['m'] = result.getMonth();
    date_parts['d'] = result.getDate();

    order = format.match( /%[Ymd]/g );
    for (var i = 0; i < order.length; i++) {
      date_parts[order[i].substr(1,1)] = parseInt(match[i]);
    };

    if (date_parts['Y'] < 100) { date_parts += 2000; };
    result = new Date(date_parts.Y, date_parts.m-1, date_parts.d);
  };
  return result;
};

function calendar_cells() {
  var table = document.getElementById('calendar_widget');
  return grep( function(i){ return i.className == 'number'}, table.getElementsByTagName('td'));
};

function add_days(date,delta) {
  // get the calendar date 24h before the current date:
  var result = new Date();
  result.setTime(date.getTime() + delta*(24 * 60 * 60 *1000) );
  if (result.getDate() == date.getDate()) {
    // Ooops - date had more than 24 hours to it (DST!)
    result.setTime( result.getTime() + (delta > 0 ? 1 : -1 ) *60 * 60 * 1000);
  };

  // Clamp the time to the original time:
  result.setHours( date.getHours());

  return result;
};

function day_before(date) { return add_days(date,-1); };
function day_after(date) { return add_days(date, 1); };
function first_of_month(date) { return new Date( date.getFullYear(), date.getMonth(), 1 ); };
function prev_month(date) { return day_before( first_of_month( date )); };
function next_month(date) { return add_days( first_of_month( date ),32 ); };
function prev_year(date) { var result = date; result.setYear( date.getFullYear() -1 ); return result; };
function next_year(date) { var result = date; result.setYear( date.getFullYear() +1 ); return result; };

var target_widget;
var current_day = new Date(2003,9,15);
var display_day = new Date();

function same_day(d1,d2) { return d1.getDate() == d2.getDate() && d1.getMonth() == d2.getMonth() && d1.getFullYear() == d2.getFullYear() };

function display_month(month) {
  if (! document.getElementById)
    return;
  var first = first_of_month( month );
  var today = new Date;

  var table = document.getElementById('calendar_widget');
  var month_year_display = document.getElementById('month_year_display');
  month_year_display.innerHTML = month_names[month.getMonth()] + " " + month.getFullYear();

  // Find the date for the upper left corner:
  var curr = day_before(first);
  while (curr.getDay() != 1) curr = day_before(curr);
  var cells = calendar_cells();
  for (var offset = 0; offset < cells.length; offset++) {
    var span_class = '';
    if (curr.getMonth() != month.getMonth()) { span_class = 'other_month' };
    if (same_day(curr,today)) { span_class = 'today' };
    if (same_day(curr,current_day)) { span_class = 'current_selection' };
    cells[offset].innerHTML = '<a href="#" onclick="javascript:day_select_click('+curr.getFullYear()+','+(curr.getMonth())+','+curr.getDate()+')">'
                            + '<span class="'+span_class+'">' + curr.getDate() + '</span></a>';
    curr = day_after( curr );
  };
  display_day = month;
};

function close_widget() {
  var parent = window.parent.document;
  parent.getElementById('calendar_widget').style.display = 'none';
};

function set_edit(widget) { target_widget = widget; current_day = strptime( date_format, target_widget.value ); display_month(current_day); };

function update_selection(date) {
  var s = date != '' ? strftime(date_format,date) : '';
  target_widget.value = s;
};

function month_left_click(widget) { display_month( prev_month( display_day )); };
function month_right_click(widget) { display_month( next_month( display_day ));};
function year_left_click(widget) { display_month( prev_year( display_day )); };
function year_right_click(widget) { display_month( next_year( display_day )); };
function clear_button_click(widget) { update_selection( '' ); close_widget(); };
function close_button_click(widget) { close_widget() };
function day_select_click(year,month,day) { 
    var selected = new Date(year,month,day); 
    update_selection(selected);
    close_widget(); };
</script>
</head>
<body onload="display_month(display_day)">
<table width="100%" height="100%" class="calendar_widget" id="calendar_widget">
<!--  &#xAB; &#x2039; &bull; &#x203A; &#xBB; -->
<tr><td id='year_left'><a href="#" onclick="javascript:year_left_click(this)">&#xAB;</td>
    <td id='month_left'><a href="#" onclick="javascript:month_left_click(this)">&#x2039;</a></td>
    <td colspan=3 id='month_year_display' class="month_year_display">Month</td>
    <td id='month_right'><a href="#" onclick="javascript:month_right_click(this)">&#x203A;</a></td>
    <td id='year_right'><a href="#" onclick="javascript:year_right_click(this)">&#xBB;</a></td></tr>
<tr><td class="weekday">M</td><td class="weekday">T</td><td class="weekday">W</td><td class="weekday">T</td><td class="weekday">F</td><td class="weekend">S</td><td class="weekend">S</td></tr>
<tr><td class="number">1</td><td class="number">2</td><td class="number">3</td><td class="number">4</td><td class="number">5</td><td class="number">6</td><td class="number">7</td></tr>
<tr><td class="number">8</td><td class="number">9</td><td class="number">10</td><td class="number">11</td><td class="number">12</td><td class="number">13</td><td class="number">14</td></tr>
<tr><td class="number">15</td><td class="number">16</td><td class="number">17</td><td class="number">18</td><td class="number">19</td><td class="number">20</td><td class="number">21</td></tr>
<tr><td class="number">22</td><td class="number">23</td><td class="number"><span class="current_selection">24</span></td><td class="number">25</td><td class="number">26</td><td class="number"><span class="today">27</span></td><td class="number">28</td></tr>
<tr><td class="number">29</td><td class="number">30</td><td class="number">31</td><td class="number">32</td><td class="number">33</td><td class="number">34</td><td class="number">35</td></tr>
<tr><td class="number">36</td><td class="number">37</td><td class="number">38</td><td class="number">39</td><td class="number">40</td><td class="number">41</td><td class="number">42</td></tr>
<tr><td colspan=4 class="clear_button_display"><a href="#" onclick="javascript:clear_button_click(this)">[clear]</a></td><td colspan=3 class="close_button_display"><a href="#" onclick="javascript:close_button_click(this)">[close]</a></td></tr>
</table>
</body>
</html>
